tidyverse PackageIn this note, we create sereral charts in Mankiw’s book “Macroeconomics, 7th ed., 2009”.
Sys.setenv(LANG="en")
dir.create("./data")
Warning: './data' already exists
library(tidyverse)
library(WDI)
wdi_cache <- WDIcache()
write_rds(wdi_cache, "./data/wdi_cache.RData")
wdi_cache <- read_rds("./data/wdi_cache.RData")
wdi_cache$series
wdi_cache$country
WDIsearch(string = "gdp", field = "name", short = TRUE, cache = NULL)
WDIsearch(string ="", field = "name, short = FALSE, cache = wdi_cache)
WDI(
country = "all",
indicator = "NY.GDP.PCAP.KD",
start = 1960,
end = NULL,
extra = FALSE,
cache = NULL,
latest = NULL,
language = "en"
)
WDIsearch(string = "gdp", field = "name", short = FALSE, cache = wdi_cache )
gdp_current <- WDI(country = "all", indicator = "NY.GDP.MKTP.CD", extra = TRUE, cache = wdi_cache)
gdp_current
gdp_current %>% filter(country == "United States") %>%
ggplot(aes(year, NY.GDP.MKTP.CD)) + geom_line() +
labs(title = "Figure 1-1 Real GDP per Person in the U.S. Economy")
WDIsearch("inflation", "name",short = FALSE, cache = wdi_cache)
inflation <- WDI(country = "all", indicator ="NY.GDP.DEFL.KD.ZG", extra=TRUE, cache=wdi_cache)
inflation %>% filter(country == "United States") %>%
ggplot(aes(year, NY.GDP.DEFL.KD.ZG)) + geom_line() +
labs(title = "Figure 1-2 The Inflation Rate in the U.S. Economy")
WDIsearch("unemployment", "name",short = FALSE, cache = wdi_cache)
unemployment <- WDI(country = "all", indicator ="SL.UEM.TOTL.NE.ZS", extra=TRUE, cache=wdi_cache)
unemployment %>% filter(country == "United States") %>%
ggplot(aes(year, SL.UEM.TOTL.NE.ZS)) + geom_line()
WDIsearch("CPI", "name",short = FALSE, cache = wdi_cache)
cpi <- WDI(country = "all", indicator ="CPTOTSAXNZGY", extra=TRUE, cache=wdi_cache)
cpi %>% filter(country == "United States") %>%
ggplot(aes(year, CPTOTSAXNZGY)) + geom_line()
gdp_cpi <- WDI(country = "all", indicator = c(gdp = "NY.GDP.DEFL.KD.ZG", cpi="CPTOTSAXNZGY"), extra=TRUE, cache=wdi_cache)
gdp_cpi %>% pivot_longer(c(gdp, cpi), names_to = "index", values_to = "value")
gdp_cpi %>% pivot_longer(c(gdp, cpi), names_to = "index", values_to = "value") %>%
filter(country == "United States") %>% drop_na(value) %>%
ggplot(aes(x = year, y = value, color = index)) + geom_line() +
labs(title = "Figure 2-3: The GDP Deflator and the CPI")
WDIsearch("Labor Force Participation Rate", "name",short = FALSE, cache = wdi_cache)
participation <- WDI(country = "all", indicator =c(male = "SL.TLF.CACT.MA.NE.ZS", female = "SL.TLF.CACT.FE.NE.ZS"), extra=TRUE, cache=wdi_cache)
participation %>% filter(country == "United States") %>%
ggplot() + geom_line(aes(x = year, y = male), col = "blue") + geom_line(aes(x = year, y = female), col = "red")
participation %>%
pivot_longer(cols = c(male, female), names_to = "gender", values_to = "rate") %>%
filter(country=="United States") %>% ggplot(aes(x = year, y = rate, color=gender)) + geom_line()
WDIsearch(string ="", field = "name, short = FALSE, cache = wdi_cache)
df <- WDI(country = "all", indicator ="", extra=TRUE, cache=wdi_cache)
WDIsearch(string ="", field = "name, short = FALSE, cache = wdi_cache)
df <- WDI(country = "all", indicator ="", extra=TRUE, cache=wdi_cache)
search_string <- "gdp" # edit the search string
WDIsearch(string =search_string, field = "name, short = FALSE, cache = wdi_cache)
chosen_indicator <- "SL.UEM.TOTL.NE.ZS"
short_name <- "unemployment"
chosen_country <- "United States"
WDI(country = "all", indicator = c(short_name = chosen_indicator), extra=TRUE, cache=wdi_cache) %>%
filter(country == chosen_country) %>%
ggplot(aes(year, short_name)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator, ": ", short_name, " - ", chosen_country),
y = short_name)
chosen_indicator <- "SL.UEM.TOTL.NE.ZS"
chosen_country <- "United States"
WDI(country = "all", indicator = c(chosen_indicator = chosen_indicator),
extra=TRUE, cache=wdi_cache) %>%
filter(country == chosen_country) %>%
ggplot(aes(year, chosen_indicator)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator, " - ", chosen_country),
y = chosen_indicator)
chosen_indicator <- "SL.UEM.TOTL.NE.ZS"
short_name <- "unemployment"
chosen_countries <- c("United States","United Kingdom", "Japan")
WDI(country = "all", indicator = c(short_name = chosen_indicator), extra=TRUE, cache=wdi_cache) %>% drop_na(short_name) %>%
filter(country %in% chosen_countries) %>%
ggplot(aes(year, short_name, col = country)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator, ": ", short_name), y = short_name)
chosen_indicator_1 <- "NY.GDP.DEFL.KD.ZG"
short_name_1 <- "gdp"
chosen_indicator_2 <- "CPTOTSAXNZGY"
short_name_2 <- "cpi"
chosen_country <- "United States"
WDI(country = "all", indicator = c(short_name_1 = chosen_indicator_1, short_name_2 = chosen_indicator_2), extra=TRUE, cache=wdi_cache) %>%
filter(country == chosen_country) %>%
pivot_longer(c(short_name_1, short_name_2), names_to = "class", values_to = "value") %>% drop_na(value) %>%
ggplot(aes(year, value, col = class)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator_1, ": ", short_name_1, "\n", chosen_indicator_2, ": ", short_name_2, " - ", chosen_country)) +
scale_color_manual(labels = c(short_name_1, short_name_2), values = scales::hue_pal()(2))
chosen_indicator_1 <- "SL.TLF.CACT.MA.NE.ZS"
short_name_1 <- "male"
chosen_indicator_2 <- "SL.TLF.CACT.FE.NE.ZS"
short_name_2 <- "female"
chosen_country <- "United States"
WDI(country = "all", indicator = c(short_name_1 = chosen_indicator_1, short_name_2 = chosen_indicator_2), extra=TRUE, cache=wdi_cache) %>%
filter(country == chosen_country) %>%
pivot_longer(c(short_name_1, short_name_2), names_to = "class", values_to = "value") %>% drop_na(value) %>%
ggplot(aes(year, value, col = class)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator_1, ": ", short_name_1, "\n", chosen_indicator_2, ": ", short_name_2, " - ", chosen_country)) +
scale_color_manual(labels = c(short_name_1, short_name_2), values = scales::hue_pal()(2))
chosen_indicator_1 <- "NY.GDP.DEFL.KD.ZG"
short_name_1 <- "gdp"
chosen_indicator_2 <- "CPTOTSAXNZGY"
short_name_2 <- "cpi"
chosen_countries <- c("United States", "France", "Japan")
WDI(country = "all", indicator = c(short_name_1 = chosen_indicator_1, short_name_2 = chosen_indicator_2), extra=TRUE, cache=wdi_cache) %>%
filter(country %in% chosen_countries) %>%
pivot_longer(c(short_name_1, short_name_2), names_to = "class", values_to = "value") %>% drop_na(value) %>%
ggplot(aes(year, value, linetype = class, col = country)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator_1, ": ", short_name_1, "\n", chosen_indicator_2, ": ", short_name_2)) +
scale_linetype_manual(labels = c(short_name_1, short_name_2), values = c("solid", "dashed"))
chosen_indicator_1 <- "SL.TLF.CACT.MA.NE.ZS"
short_name_1 <- "male"
chosen_indicator_2 <- "SL.TLF.CACT.FE.NE.ZS"
short_name_2 <- "female"
chosen_countries <- c("United States", "France", "Japan")
WDI(country = "all", indicator = c(short_name_1 = chosen_indicator_1, short_name_2 = chosen_indicator_2), extra=TRUE, cache=wdi_cache) %>%
filter(country %in% chosen_countries) %>%
pivot_longer(c(short_name_1, short_name_2), names_to = "class", values_to = "value") %>% drop_na(value) %>%
ggplot(aes(year, value, linetype = class, col = country)) + geom_line() +
labs(title = paste("WDI ", chosen_indicator_1, ": ", short_name_1, "\n", chosen_indicator_2, ": ", short_name_2)) +
scale_linetype_manual(labels = c(short_name_1, short_name_2), values = c("solid", "dashed"))
theme_minimal()gdp_cpi %>% pivot_longer(c(gdp, cpi), names_to = "index", values_to = "value") %>%
filter(country == "United States") %>% drop_na(value) %>%
ggplot(aes(x = year, y = value, color = index)) + geom_line() +
labs(title = "Figure 2-3: The GDP Deflator and the CPI") +
theme_minimal()
tidyverse for EDAEDA is known as Exploratory Data Analysis. For the detail, please refer to “R for Data Scinece” by H. Wickham et. al. There are many excellent books available at the bookdown site for free. See also the arxive page.